/**
 * 
 */
package org.cassandra.sajith.myapp.util;

import static org.cassandra.sajith.myapp.util.Constants.KEYSPACE;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
 * @author wso2
 *
 */
public class Connector {

	TTransport transport = new TSocket("localhost", 9160);
	
	// A new connection to our keyspace

	public Cassandra.Client connect() throws InvalidRequestException, TException {
		
		TFramedTransport framedTransport = new TFramedTransport(transport);
		TProtocol protocol = new TBinaryProtocol(framedTransport);
		Cassandra.Client client = new Cassandra.Client(protocol);
		transport.open();		
		client.set_keyspace(KEYSPACE);
		
		return client;
	}
	
	public void close() {
		transport.close();
	}
}
